// pages/subcribe/subcribe.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    filter: [{
        filterName: "订阅岗位①",
        content: []
      },
      {
        filterName: "订阅岗位②",
        content: []
      },
      {
        filterName: "订阅岗位③",
        content: []
      }
    ],
    neverSubcribe: false,
    leftCount: ""
  },

  goToFilter(e) {
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../subcribeSort/subcribeSort?index=' + index,
    })
  },
  subscribe() {
    wx.showLoading({
      mask: true
    })
    wx.requestSubscribeMessage({
      tmplIds: ['mWomggCTNiBmaxG2zAoybrn0L4J4qMzttX2cqgWb1pI'],
      success(res) {
        console.log(res)
      }
    })
    let data = this.data.filter.map((item) => {
      if (item.content == undefined || item.content.length == 0) {
        return {
          city: "",
          subject: "",
          post_level: "",
          has_organized: "",
        };
      }
      return {
        city: item.content[0],
        post_level: item.content[1],
        subject: item.content[2],
        has_organized: item.content[3],
      }
    })
    console.log(data)
    if (this.data.neverSubcribe) {
      wx.$cloud.callFunction({
        name: "addSubscribe",
        data: {
          "data": data,
          openid: getApp().globalData.openId,
        }
      }).then(res => {
        this.setData({
          neverSubcribe: false
        })
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          icon: "success",
          title:"订阅成功"
        })
      })
    } else {
      wx.$cloud.callFunction({
        name: "changeSubscribe",
        data: {
          "data": data,
          openid: getApp().globalData.openId,
        }
      }).then(res => {
        console.log(res)
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          icon: "success",
          title:"订阅成功"
        })
      })
      // }

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    console.log("openid:",getApp().globalData.openId)
    //获取数据库数据
    let result = await wx.$cloud.callFunction({
      name: "checkSubscribe",
      data: {
        openid: getApp().globalData.openId
      }
    })
    console.log("check", result)
    let data;
    if (result.result.is_subscribe == false) {
      this.setData({
        neverSubcribe: true
      })
    } else {
      console.log(data)
      data = result.result.data
      data = data.map((item) => {
        return [item.city || item.city === "" ? item.city : "不限", item.subject || item.subject === "" ? item.subject : "不限", item.post_level || item.post_level === "" ? item.post_level : "不限", item.has_organized || item.has_organized === "" ? item.has_organized : "不限"]
      })
    }
    console.log(data)
    // //获取全局变量
    // for (let i = 0; i < getApp().globalData.chooseTags.length; i++) {
    //   if (getApp().globalData.chooseTags[i].length===0)
    //     getApp().globalData.chooseTags[i] = data[i]
    // }
    let chooseTags = getApp().globalData.chooseTags
    console.log("用户选择的Tag：", chooseTags)
    let filter = this.data.filter;
    if (this.data.neverSubcribe) {
      filter = filter.map((item, index) => {
        item.content = chooseTags[index];
        return item;
      })
      this.setData({
        filter: filter
      })
    } else {
      filter = filter.map((item, index) => {
        console.log(item)
        item.content = chooseTags[index].length == 0 ? data[index] : chooseTags[index];
        return item;
      })
      for(let i=0;i<filter.length;i++){
        chooseTags[i] = filter[i].content
      } 
      getApp().globalData.chooseTags = chooseTags
      this.setData({
        filter: filter
      })
    }
   
    let leftCount = await wx.$cloud.callFunction({
      name: "doSub",
      data: {
        sign: 1,
        openid: getApp().globalData.openId
      }
    })
    console.log(leftCount)
    this.setData({
      leftCount: leftCount.result.subNumber
    })
  },
  addSubscribe() {
    const that = this;
    console.log("addddddddddddddddddddd")
    wx.requestSubscribeMessage({
      tmplIds: ['mWomggCTNiBmaxG2zAoybrn0L4J4qMzttX2cqgWb1pI'],
      success(res) {
        console.log(res)
        let leftCount = ~~(that.data.leftCount) + 1;
        that.setData({
          leftCount: leftCount
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log(getApp().globalData.openId)
    wx.$cloud.callFunction({
      name: "doSub",
      data: {
        sign: 0,
        openid: getApp().globalData.openId,
        subNumber: this.data.leftCount
      }
    }).then(res => {
      console.log("success")
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.$cloud.callFunction({
      name: "doSub",
      data: {
        sign: 0,
        openid: getApp().globalData.openId,
        subNumber: this.data.leftCount
      }
    }).then(res => {
      console.log("success")
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})